home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mastering Internet Develo…oft ActiveX Technologies
/
Mastering Internet Development with ActiveX (1996)(Microsoft).iso
/
labs
/
lab10.2
/
solution
/
echo.cpp
next >
Wrap
C/C++ Source or Header
|
1996-07-16
|
5KB
|
181 lines
// ECHO.CPP - Implementation file for your Internet Server
// Echo Extension
#include <afx.h>
#include <afxwin.h>
#include <afxisapi.h>
#include "resource.h"
#include "Echo.h"
///////////////////////////////////////////////////////////////////////
// command-parsing map
BEGIN_PARSE_MAP(CEchoExtension, CHttpServer)
// TODO: insert your ON_PARSE_COMMAND() and
// ON_PARSE_COMMAND_PARAMS() here to hook up your commands.
// For example:
//Lab 10.2, ex 2 - Implementing Echo
ON_PARSE_COMMAND(EchoRequest, CEchoExtension, ITS_PSTR)
ON_PARSE_COMMAND_PARAMS("option=full")
ON_PARSE_COMMAND(Default, CEchoExtension, ITS_EMPTY)
DEFAULT_PARSE_COMMAND(Default, CEchoExtension)
END_PARSE_MAP(CEchoExtension)
///////////////////////////////////////////////////////////////////////
// The one and only CEchoExtension object
CEchoExtension theExtension;
///////////////////////////////////////////////////////////////////////
// CEchoExtension implementation
CEchoExtension::CEchoExtension()
{
}
CEchoExtension::~CEchoExtension()
{
}
BOOL CEchoExtension::GetExtensionVersion(HSE_VERSION_INFO* pVer)
{
// Call default implementation for initialization
CHttpServer::GetExtensionVersion(pVer);
// Load description string
TCHAR sz[HSE_MAX_EXT_DLL_NAME_LEN+1];
ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
IDS_SERVER, sz, HSE_MAX_EXT_DLL_NAME_LEN));
_tcscpy(pVer->lpszExtensionDesc, sz);
return TRUE;
}
///////////////////////////////////////////////////////////////////////
// CEchoExtension command handlers
void CEchoExtension::Default(CHttpServerContext* pCtxt)
{
StartContent(pCtxt);
WriteTitle(pCtxt);
*pCtxt << "<P><H1>Help for Echo.dll</H1><P><HR>";
EchoHelp(pCtxt);
EndContent(pCtxt);
}
//Lab 10.2, ex 2 - Implementing Echo
void CEchoExtension::EchoRequest(CHttpServerContext* pCtxt, LPCTSTR pstrOption)
{
StartContent(pCtxt);
WriteTitle(pCtxt);
*pCtxt << "<P><H1>Echo.dll</H1><P><HR>";
if ( _stricmp(pstrOption, "full") == 0)
{
EchoHead(pCtxt);
*pCtxt << "<BR>";
EchoBody(pCtxt);
}
else if ( _stricmp(pstrOption, "header") == 0)
{
EchoHead(pCtxt);
}
else if ( _stricmp(pstrOption, "body") == 0)
{
EchoBody(pCtxt);
}
else
BadSyntax(pCtxt);
EndContent(pCtxt);
}
///////////////////////////////////////////////////////////////////////
// Implementation member functions
//Lab 10.2, ex 2 - Implementing Echo
void CEchoExtension::EchoHead(CHttpServerContext* pCtxt)
{
char pstrBuffer[1000];
DWORD dwSize = sizeof(pstrBuffer);
*pCtxt << "<P><H3>Request Header Information</H3>";
*pCtxt << "<BR>Request method: " << pCtxt->m_pECB->lpszMethod;
*pCtxt << "<BR>Tranlsated path: " << pCtxt->m_pECB->lpszPathTranslated;
pCtxt->GetServerVariable("QUERY_STRING", pstrBuffer, &dwSize);
*pCtxt << "<BR>Query String: " << pstrBuffer;
dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
pCtxt->GetServerVariable("REMOTE_ADDR", pstrBuffer, &dwSize);
*pCtxt << "<BR>Client IP address: " << pstrBuffer;
}
void CEchoExtension::EchoBody(CHttpServerContext* pCtxt)
{
char pstrBuffer[4000];
DWORD dwSize = sizeof(pstrBuffer);
*pCtxt << "<P><H3>Request Body Information</H3><BR>";
BOOL b = pCtxt->ReadClient(pstrBuffer, &dwSize);
*pCtxt << "Body is " << (long int)dwSize << " bytes <BR>" ;
if (b == TRUE)
*pCtxt << pstrBuffer;
}
void CEchoExtension::BadSyntax(CHttpServerContext* pCtxt)
{
*pCtxt << "<H3>Bad Syntax Error!</H3><P>";
*pCtxt << "Proper syntax is as follows:";
EchoHelp(pCtxt);
}
void CEchoExtension::EchoHelp(CHttpServerContext* pCtxt)
{
*pCtxt << "<P><I>Supported method:</I>";
*pCtxt << "<BR><B><TT>EchoRequest</TT></B> - currently the only method supported."
<< " Dynamically creates an HTML page containing HTTP request information.";
*pCtxt << "<P><I>Supported arguments:</I>";
*pCtxt << "<BR><B><TT>Full</TT></B> - echo back the entire HTTP request message.";
*pCtxt << "<BR><B><TT>Body</TT></B> - echo back only the HTTP request message body.";
*pCtxt << "<BR><B><TT>Header</TT></B> - echo back only the HTTP request message head.";
}
///////////////////////////////////////////////////////////////////////
// If your extension will not use MFC, you'll need this code to make
// sure the extension objects can find the resource handle for the
// module. If you convert your extension to not be dependent on MFC,
// remove the comments arounn the following AfxGetResourceHandle()
// and DllMain() functions, as well as the g_hInstance global.
/****
static HINSTANCE g_hInstance;
HINSTANCE AFXISAPI AfxGetResourceHandle()
{
return g_hInstance;
}
BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
LPVOID lpReserved)
{
if (ulReason == DLL_PROCESS_ATTACH)
{
g_hInstance = hInst;
}
return TRUE;
}
****/